'''urllib_request的定制请求携带headers
请求对象的定制Request 是解决反爬的第一种手段！！ '''
import urllib.request

url = 'https://www.baidu.com'

# 请求定制Request
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'
}
request = urllib.request.Request(url=url, headers=headers)
# 将自定义的request放入urlopen()
response = urllib.request.urlopen(request)
# 读取返回内容并转码
content = response.read().decode('utf-8')
print(content)
